package ait.mediabroadcast.mediaserver;

import java.util.HashMap;

import org.teleal.cling.support.model.WriteStatus;
import org.teleal.cling.support.model.container.Container;

public class AndroidContentTree {

	public final static String ROOT_ID = "0";
	public final static String VIDEO_ID = "1";
	public final static String AUDIO_ID = "2";
	public final static String VIDEO_PREFIX = "video-item-";
	public final static String AUDIO_PREFIX = "audio-item-";
	
	private final static String LOGTAG = AndroidContentTree.class.toString();
	
	private static HashMap<String, AndroidContentNode> childrenContentMap = new HashMap<String, AndroidContentNode>();
	private static AndroidContentNode rootNode = createRootNode();
	
	static AndroidContentNode createRootNode() {
		
		Container root = new Container();
		root.setId(ROOT_ID);
		root.setParentID("-1");
		root.setTitle("MediaServer root directory");
		root.setCreator("Media Server");
		root.setRestricted(true);	// read only
		root.setSearchable(true);
		root.setWriteStatus(WriteStatus.NOT_WRITABLE);
		root.setChildCount(0);
		AndroidContentNode rootNode = new AndroidContentNode(ROOT_ID, root);
		childrenContentMap.put(ROOT_ID, rootNode);
		return rootNode;
	}
	
	public static AndroidContentNode getRootNode() {
		if (rootNode == null)
			rootNode = createRootNode();
		return rootNode;
	}
	
	public static void clearTree() {
		rootNode = null;
		childrenContentMap.clear();
	}
	
	public static AndroidContentNode getNode(String id) {
		if (childrenContentMap.containsKey(id)) {
			return childrenContentMap.get(id);
		}
		return null;
	}
	
	public static boolean hasNode(String id) {
		return childrenContentMap.containsKey(id);
	}
	
	public static void addNode(String id, AndroidContentNode node) {
		if (!hasNode(id)) {
			childrenContentMap.put(id, node);
		}
	}
}
